package com.army.modules.host.job.listener;

import com.army.modules.host.entity.HostWater;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Service
public class FileToDataWriter implements ItemWriter<HostWater> {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public void write(List<? extends HostWater> list) throws Exception {
        String sql="INSERT INTO host_water " +
                "( global_no, core_no, trans_date, trans_time, borrow_acct, loans_acct, trans_result )" +
                "VALUES( ?, ?, ?, ?, ?, ?, ? )";

        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                HostWater hostWater = list.get(i);
                preparedStatement.setString(1,hostWater.getGlobalNo());
                preparedStatement.setString(2,hostWater.getCoreNo());
                preparedStatement.setString(3,hostWater.getTransDate());
                preparedStatement.setString(4,hostWater.getTransTime());
                preparedStatement.setString(5,hostWater.getBorrowAcct());
                preparedStatement.setString(6,hostWater.getLoansAcct());
                preparedStatement.setString(7,hostWater.getTransResult());
            }

            @Override
            public int getBatchSize() {
                return list.size();
            }
        });


    }
}
